local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local string = string
local math = math
local tonumber = tonumber
local tostring = tostring
local os = os
local conf = require "conf"
local util = require "util"
package.cpath = "/usr/nginx/?.so;;"
local json = require 'cjson'
module(...)

_VERSION = '0.1'

function send(params)
    local http = require("http")
    local url = 'http://inner.jetgocloud.com/sms.php'
    
    url = url .. '?' ..  string.gsub(ngx.encode_args(params), ",", "%%2c")
    ngx.log(ngx.ERR, url)
    -- ngx.log(ngx.ERR, json.encode(ngx.decode_args('Signature=ka8PDlV7S9sYqxEMRnmlBv%2FDoAE%3D&AccessKeyId=testid&Action=SingleSendSms&Format=XML&ParamString={"name":"d","name1":"d"}&RecNum=13098765432&RegionId=cn-hangzhou&SignName=标签测试&SignatureMethod=HMAC-SHA1&SignatureNonce=9e030f6b-03a2-40f0-a6ba-157d44532fd0&SignatureVersion=1.0&TemplateCode=SMS_1650053&Timestamp=2016-10-20T05:37:52Z&Version=2016-09-27')))
    local ok, code, _, _, body  = http:request {
        url = url,
        timeout = 6000,
        method = "GET",
        headers = {  
            ["Content-Type"] = "application/x-www-form-urlencoded"
        }
        -- body = ngx.encode_args(params)
    }
    ngx.log(ngx.ERR, ok)
    ngx.log(ngx.ERR, body)

    return code
end

function default(args)
    local conf = require "conf"
    if args.action == 'send' then
        send(args)
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)
